package com.pqc.hello.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p> 自动配置。</p>
 *
 * @author panqingcui <br>
 * @create 2018-06-28 10:32 <br>
 */
@Configuration //标识 当前类是配置类
@EnableConfigurationProperties(value = HelloStarterProperties.class) //启动配置文件，value用来指定我们要启用的配置类，可以有多个，多个时我们可以这么写value={xxProperties1.class,xxProperteis2.class....}
@ConditionalOnClass(HelloStarterService.class) //该注解的参数对应的类必须存在，否则不解析该注解修饰的配置类
@ConditionalOnProperty(prefix = "starter", value = "enable", matchIfMissing = false) //表示只有我们的配置文件是否配置了以starter为前缀的资源项值，并且在该资源项值为enable，如果没有配置我们默认设置为enable
public class HelloStarterServiceAutoConfiguration {
    @Autowired
    private HelloStarterProperties helloStarterProperties;

    @Bean
    @ConditionalOnMissingBean(HelloStarterService.class)//该注解表示，如果存在它修饰的类的bean，则不需要再创建这个bean
    public HelloStarterService helloService() {
        HelloStarterService helloStarterService = new HelloStarterService();
        helloStarterService.setDescr(helloStarterProperties.getDescr());
        return helloStarterService;
    }
}
